//
//  SpecailGuiderIntroduceCell.m
//  ImGuider
//
//  Created by 王鹏宇 on 2017/12/29.
//  Copyright © 2017年 imguider. All rights reserved.
//

#import "SpecailGuiderIntroduceCell.h"
#import "RewardView.h"
#import "ChargeMoneyVC.h"
#import "RewardRecordVC.h"
#import "GuiderDetailVC.h"

@interface SpecailGuiderIntroduceCell () <RewardViewDelegate>

@property (weak, nonatomic) IBOutlet UILabel *SpecailNameLabel;
@property (weak, nonatomic) IBOutlet GuiderIconView *guiderView;

@property (weak, nonatomic) IBOutlet UILabel *guiderNameLabel;

@property (weak, nonatomic) IBOutlet UIView *rewardBtnView;
@property (weak, nonatomic) IBOutlet UILabel *rewardLabel;

@property (weak, nonatomic) IBOutlet YYLabel *introduceLabel;

@property (weak, nonatomic) IBOutlet UILabel *sessionsLabel;
@property (weak, nonatomic) IBOutlet UILabel *lisentCountLabel;
@property (weak, nonatomic) IBOutlet UILabel *collectionCountLabel;

@property (weak, nonatomic) IBOutlet UILabel *commentsLabel;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *introduceHeight;

@property (nonatomic, strong)RewardView * rewardView;

@end

@implementation SpecailGuiderIntroduceCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    self.selectionStyle =  UITableViewCellSelectionStyleNone;
    self.rewardLabel.text = MYLocalizedString(@"COIN_REWARD", nil);
    
    @weakify(self);
    [self.rewardBtnView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        
        @strongify(self);
        
        if (![IGUtil hasLoginOrPresentLogin]) {
            
            return;
        }
        
        self.superVC.sl_popupController.layoutType = PopupLayoutTypeBottom;
        [self.superVC.sl_popupController presentContentView:self.rewardView];
        
        self.rewardView.lineid = self.special.lineid;
    }]];
}
- (IBAction)showGuiderDetail:(id)sender {
    
    GuiderDetailVC *guiderVC = [GuiderDetailVC new];
    
    guiderVC.guiderID = self.special.guideid;
    
    [self.superVC.navigationController pushViewController:guiderVC animated:YES];
}

- (RewardView *)rewardView {
    
    if (!_rewardView) {
        
        _rewardView = [[RewardView alloc] initWithFrame:CGRectMake(0, (kScreenHeight - RewardH), kScreenWidth, RewardH)];
        _rewardView.delegate = self;
    }
    
    return _rewardView;
}


- (void)setSpecial:(LineModel *)special {
    
    _special = special;
    
    if (!special) {
        return;
    }
    
    self.SpecailNameLabel.text = special.linename;
    
    self.guiderView.headerImageName = special.guide.headimg;
    self.guiderView.vip = special.guide.vip;
    self.guiderNameLabel.text = special.guide.realname;
    
    self.introduceLabel.text = special.linedesc;
    
    self.sessionsLabel.text = special.recordcount;
    self.lisentCountLabel.text = special.visits;
    self.collectionCountLabel.text = special.favourcount;
    self.commentsLabel.text = special.commentnum;
    
    [self setIntroText];
}


- (void)setIntroText {
    
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:self.special.linedesc.length > 0 ? self.special.linedesc : @" "];
    text.font = Font(12);
    text.color = kContentColor;
    text.lineSpacing = 8;
    
    self.introduceLabel.attributedText = text;
    
    if (self.special.hasShowAll) {
        self.introduceLabel.numberOfLines = 0;
        
        self.introduceHeight.constant = [self.introduceLabel sizeThatFits:CGSizeMake(kScreenWidth - 32, CGFLOAT_MAX)].height;

        return;
    }
    
    self.introduceLabel.numberOfLines = 3;
    self.introduceHeight.constant = [self.introduceLabel sizeThatFits:CGSizeMake(kScreenWidth - 32, CGFLOAT_MAX)].height;
    
    NSMutableAttributedString *moreText = [[NSMutableAttributedString alloc] initWithString:[@"..." stringByAppendingString:MYLocalizedString(@"LINEDETAIL_SECTION_DETAIL", nil)]];
    
    moreText.font = Font(12);
    moreText.color = kContentColor;
    moreText.lineSpacing = 8;
    
    [moreText setColor:kThemeColor range:[moreText.string rangeOfString:MYLocalizedString(@"LINEDETAIL_SECTION_DETAIL", nil)]];
    
    YYTextHighlight *hiText = [YYTextHighlight new];
    
    [hiText setTapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        
        self.special.hasShowAll = YES;
        [self.tableView reloadData];
    }];
    
    [moreText setTextHighlight:hiText range:[moreText.string rangeOfString:MYLocalizedString(@"LINEDETAIL_SECTION_DETAIL", nil)]];
    
    
    YYLabel *seeMore = [YYLabel new];
    seeMore.attributedText = moreText;
    [seeMore sizeToFit];
    
    NSAttributedString *truncationToken = [NSAttributedString attachmentStringWithContent:seeMore contentMode:UIViewContentModeCenter attachmentSize:seeMore.size alignToFont:moreText.font alignment:YYTextVerticalAlignmentCenter];
    self.introduceLabel.truncationToken = truncationToken;
}

#pragma mark  -- RewardViewDelegate

- (void)recharge {
    
    ChargeMoneyVC * moneyVC = [IGUtil loadViewController:@"ChargeMoneyVC" storyboard:@"Account"];
    
    [self.superVC.sl_popupController dismiss];
    [self.superVC.navigationController pushViewController:moneyVC animated:YES];
}

- (void)rewardWithModel:(RewardModel *)model {
    
    [NetAPIManager rewardWithLineid:self.special.lineid coin:model.amount block:^(NSString *result, NSError *error) {
        
        if (!error) {
            [[NSNotificationCenter defaultCenter] postNotificationName:kReloadUserDataNotif object:nil];
            [IGUtil showToast:MYLocalizedString(@"REWARD_SUCCESS", nil)];
            [self.superVC.sl_popupController dismiss];
        }
    }];
}

- (void)showRewardRecord {
    
    RewardRecordVC *recordVC = [RewardRecordVC new];
    recordVC.unionid = self.special.unionid;
    [self.superVC.sl_popupController dismiss];
    
    [self.superVC.navigationController pushViewController:recordVC animated:YES];
}

@end
